import Tape
import Transition

class MachineStuck(Exception):
    pass

class MachineFinished(Exception):
    pass

class TuringMachine:
        """A turing machine with the specified transition table, list of final states, tape and initial state"""
        def __init__(self, transition,  tape, blank, initialState, finalStates):
                self.tape=Tape.Tape(blank, tape)
                self.transition = transition
                self.currentState = initialState
                self.finalStates=finalStates
                
        def isRunning(self, currentValue):
                return currentValue in self.transition[self.currentState]
            
        def isFinal(self):
            return self.currentState in self.finalStates
        def next(self):
                if self.isRunning(self.tape.currentValue):
                        print("old values",self.currentState, self.tape.currentValue)
                        self.currentState, self.tape.currentValue, step = self.transition[self.currentState][self.tape.currentValue]
                        print("new values",self.currentState, self.tape.currentValue)
                        self.tape.move(step)
                else:
                        if self.isFinal():
                            raise MachineFinished()
                        else:
                            raise MachineStuck()
                    